home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 210 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  16.1 KB

  1. Path: lugb.latrobe.edu.au!lux!cs102238
  2. From: cs102238@lux.latrobe.edu.au (Gregary John Boyles )
  3. Newsgroups: alt.msdos.programmer,comp.lang.c
  4. Subject: Pascal program works but not C program!
  5. Date: 3 Jan 1996 11:38:10 GMT
  6. Organization: La Trobe University
  7. Distribution: world
  8. Message-ID: <4cdpr2$psi@lugb.latrobe.edu.au>
  9. NNTP-Posting-Host: lux.latrobe.edu.au
  10.  
  11. EMail : boyles@lux.latrobe.edu.au
  12.         
  13. Why does the Pascal version of this program behave exactly as expected while the
  14. C++ version does not?
  15.  
  16.  
  17.  
  18.  
  19. ******************************* PASCAL VERSION *******************************
  20.  
  21. program show;
  22.  
  23.  
  24. uses crt,dos,crt_,strings;
  25.  
  26.  
  27. const maxstring=127;
  28.       escape=chr(27);
  29.       space=' ';
  30.       up='H';
  31.       down='P';
  32.       pageup='I';
  33.       pagedown='Q';
  34.       left='K';
  35.       right='M';
  36.       home='G';
  37.       end_='O';
  38.       null=chr(0);
  39.  
  40.  
  41. type nodetypeptr=^nodetype;
  42.      nodetype=record
  43.                    fileline:string;
  44.                    filelinenum:integer;
  45.                    next,previous:nodetypeptr;
  46.               end;
  47.  
  48.  
  49. function empty(posptr:nodetypeptr):boolean;
  50.  
  51. begin
  52.      empty:=posptr=nil;
  53. end;
  54.  
  55.  
  56. procedure initialize(var listptr:nodetypeptr);
  57.  
  58. begin
  59.      listptr:=nil;
  60. end;
  61.  
  62.  
  63. procedure add(var listptr:nodetypeptr;fileline:string;filelinenum:integer);
  64.  
  65. var newptr,tempptr:nodetypeptr;
  66.  
  67. begin
  68.       new(newptr);
  69.       newptr^.fileline:=fileline;
  70.       newptr^.filelinenum:=filelinenum;
  71.       if (listptr=nil) then
  72.       begin
  73.            (listptr):=newptr;
  74.            (listptr)^.next:=nil;
  75.            (listptr)^.previous:=nil;
  76.       end
  77.       else
  78.       begin
  79.            tempptr:=listptr;
  80.            while (tempptr^.next<>nil) do
  81.            begin
  82.                 tempptr:=tempptr^.next;
  83.            end;
  84.            tempptr^.next:=newptr;
  85.            newptr^.next:=nil;
  86.            newptr^.previous:=tempptr;
  87.       end;
  88. end;
  89.  
  90.  
  91. procedure remove(var listptr:nodetypeptr);
  92.  
  93. var tempptr:nodetypeptr;
  94.  
  95. begin
  96.      tempptr:=listptr;
  97.      listptr:=listptr^.next;
  98.      listptr^.previous:=nil;
  99.      dispose(tempptr);
  100. end;
  101.  
  102.  
  103. procedure setpos(var posptr:nodetypeptr;filelinenum:integer);
  104.  
  105. begin
  106.      while (posptr^.filelinenum<>filelinenum) do
  107.      begin
  108.           if (posptr^.filelinenum>filelinenum) then
  109.           begin
  110.                posptr:=posptr^.previous;
  111.           end
  112.           else if (posptr^.filelinenum<filelinenum) then
  113.           begin
  114.                posptr:=posptr^.next;
  115.           end;
  116.      end;
  117. end;
  118.  
  119.  
  120. procedure writescreenfull(width,height:word;posptr:nodetypeptr;stringpos:word);
  121.  
  122. var y,i:word;
  123.     line1:string;
  124.  
  125. begin
  126.      window(1,1,width,height);
  127.      clrscr;
  128.      window(1,1,width,height+1);
  129.      y:=1;
  130.      while (y<=height) and (not empty(posptr^.next)) do
  131.      begin
  132.           gotoxy(1,y);
  133.           i:=stringpos;
  134.           while (i<=length(posptr^.fileline)) and ((i-stringpos)<=width) do
  135.           begin
  136.                write(posptr^.fileline[i]+'');
  137.                i:=i+1;
  138.           end;
  139.           writeln;
  140.           posptr:=posptr^.next;
  141.           y:=y+1;
  142.      end;
  143. end;
  144.  
  145.  
  146. procedure main;
  147.  
  148. var infile:text;
  149.     x,width,height,back,fore:word;
  150.     ch:char;
  151.     numfilelines,filelinenum:integer;
  152.     listptr,posptr:nodetypeptr;
  153.     fileline,bottomfileline:string;
  154.     stringpos:word;
  155.  
  156. begin
  157.      if (paramcount<1) then
  158.      begin
  159.           writeln;
  160.           writeln('Show what . . .');
  161.      end
  162.      else
  163.      begin
  164.           {$I-}
  165.           assign(infile,paramstr(1));
  166.           {$I+}
  167.           if (doserror<>0) then
  168.           begin
  169.                writeln;
  170.                write('infile ');
  171.                write(paramstr(1));
  172.                writeln(' not found . . .');
  173.           end
  174.           else
  175.           begin
  176.                reset(infile);
  177.                initialize(listptr);
  178.                filelinenum:=0;
  179.                while (not eof(infile)) do
  180.                begin
  181.                     readln(infile,fileline);
  182.                     add(listptr,fileline,filelinenum);
  183.                     filelinenum:=filelinenum+1;;
  184.                end;
  185.                numfilelines:=filelinenum-1;
  186.                close(infile);
  187.                posptr:=listptr;
  188.                fore:=textattr and $0f;
  189.                back:=(textattr and $70) div $10;
  190.                height:=hi(windmax);
  191.                width:=lo(windmax);
  192.                bottomfileline:=' '+chr(24)+' '+chr(25)+' pgup  pgdn  s "string" : (search for string)  esc : (quit)';
  193.                for x:=length(bottomfileline)+1 to width do
  194.                begin
  195.                     bottomfileline:=bottomfileline+' ';
  196.                end;
  197.                set_cursor(none);
  198.                clrscr;
  199.                textcolor(back);
  200.                textbackground(fore);
  201.                gotoxy(1,height);
  202.                write(bottomfileline);
  203.                textcolor(fore);
  204.                textbackground(back);
  205.                height:=height-1;
  206.                filelinenum:=1;
  207.                stringpos:=1;
  208.                writescreenfull(width,height,posptr,stringpos);
  209.                ch:=space;
  210.                while (ch<>escape) do
  211.                begin
  212.                     while ((ch<>up) and (ch<>down) and (ch<>pageup) and
  213.                            (ch<>pagedown) and (ch<>escape) and (ch<>left) and
  214.                            (ch<>right) and (ch<>end_) and (ch<>home)) do
  215.                     begin
  216.                          ch:=readkey;
  217.                          if (ch=null) then
  218.                          begin
  219.                               ch:=readkey;
  220.                          end;
  221.                     end;
  222.                     if (ch=up) then
  223.                     begin
  224.                          if ((filelinenum-1)>=1) then
  225.                          begin
  226.                               filelinenum:=filelinenum-1;
  227.                          end
  228.                          else
  229.                          begin
  230.                               filelinenum:=1;
  231.                          end;
  232.                          setpos(posptr,filelinenum);
  233.  
  234.                     end
  235.                     else if (ch=down) then
  236.                     begin
  237.                          if ((filelinenum+1+height)<=numfilelines) then
  238.                          begin
  239.                               filelinenum:=filelinenum+1
  240.                          end
  241.                          else
  242.                          begin
  243.                               filelinenum:=numfilelines-height+1;
  244.                          end;
  245.                          setpos(posptr,filelinenum);
  246.                     end
  247.                     else if (ch=pageup) then
  248.                     begin
  249.                          if ((filelinenum-height+1)>=1) then
  250.                          begin
  251.                               filelinenum:=filelinenum-height+1;
  252.                          end
  253.                          else
  254.                          begin
  255.                               filelinenum:=1;
  256.                          end;
  257.                          setpos(posptr,filelinenum);
  258.                     end
  259.                     else if (ch=pagedown) then
  260.                     begin
  261.                          if (((filelinenum+height-1)<=numfilelines) and
  262.                              ((numfilelines-(filelinenum+height-1))>=(height-1))) then
  263.                          begin
  264.                               filelinenum:=filelinenum+height-1;
  265.                          end
  266.                          else
  267.                          begin
  268.                               filelinenum:=numfilelines-height+1;
  269.                          end;
  270.                          setpos(posptr,filelinenum);
  271.                     end
  272.                     else if (ch=left) then
  273.                     begin
  274.                          if (stringpos>1) then
  275.                          begin
  276.                               stringpos:=stringpos-1;
  277.                          end;
  278.                     end
  279.                     else if (ch=right) then
  280.                     begin
  281.                          if (stringpos<=(maxstring-width)) then
  282.                          begin
  283.                               stringpos:=stringpos+1;
  284.                          end;
  285.                     end
  286.                     else if (ch=end_) then
  287.                     begin
  288.                          stringpos:=maxstring-width+1;
  289.                     end
  290.                     else if (ch=home) then
  291.                     begin
  292.                          stringpos:=1;
  293.                     end;
  294.                     if (ch<>escape) then
  295.                     begin
  296.                          ch:=space;
  297.                     end;
  298.                     writescreenfull(width,height,posptr,stringpos);
  299.                end;
  300.                height:=height+1;
  301.                window(1,1,width,height);
  302.                clrscr;
  303.                set_cursor(line);
  304.                while (not empty(listptr)) do
  305.                begin
  306.                     remove(listptr);
  307.                end;
  308.           end;
  309.      end;
  310. end;
  311.  
  312.  
  313. begin
  314.      main;
  315. end.
  316.  
  317.  
  318.  
  319.  
  320. ******************************** C++ VERSION *********************************
  321.  
  322. #include <stdio.h>
  323. #include <conio.h>
  324. #include <string.h>
  325. #include <alloc.h>
  326. #include <process.h>
  327.  
  328. const maxstring=127;
  329. const lineend='\n';
  330. const escape=27;
  331. const null='\0';
  332. const space=' ';
  333. const up='H';
  334. const down='P';
  335. const pageup='I';
  336. const pagedown='Q';
  337. const left='K';
  338. const right='M';
  339. const home='G';
  340. const end_='O';
  341.  
  342.  
  343. typedef char string[maxstring];
  344. typedef struct nodetypetag{string line;int linenum;struct nodetypetag *next,*previous;} nodetype;
  345. typedef nodetype *nodetypeptr;
  346.  
  347. string line,line_,line1,bottomline;
  348.  
  349. char empty(nodetypeptr posptr)
  350. {
  351.      return(posptr==NULL);
  352. }
  353.  
  354. void initialize(nodetypeptr *listptr)
  355. {
  356.      *listptr=NULL;
  357. }
  358.  
  359. void add(nodetypeptr *listptr,char *fileline,int Linenum)
  360. {
  361.       nodetypeptr newptr,tempptr;
  362.  
  363.       newptr=(nodetypeptr)malloc(sizeof(nodetype));
  364.       strcpy(newptr->line,fileline);
  365.       newptr->linenum=Linenum;
  366.       if (*listptr==NULL)
  367.       {
  368.            (*listptr)=newptr;
  369.            (*listptr)->next=NULL;
  370.            (*listptr)->previous=NULL;
  371.       }
  372.       else
  373.       {
  374.            tempptr=*listptr;
  375.            while (tempptr->next!=NULL)
  376.            {
  377.                 tempptr=tempptr->next;
  378.            }
  379.            tempptr->next=newptr;
  380.            newptr->next=NULL;
  381.            newptr->previous=tempptr;
  382.       }
  383. }
  384.  
  385. void remove(nodetypeptr *listptr)
  386. {
  387.      nodetypeptr tempptr;
  388.  
  389.      tempptr=*listptr;
  390.      *listptr=(*listptr)->next;
  391.      (*listptr)->previous=NULL;
  392.      free(tempptr);
  393. }
  394.  
  395. void setpos(nodetypeptr *posptr,int linenum)
  396. {
  397.      while ((*posptr)->linenum!=linenum)
  398.      {
  399.           if ((*posptr)->linenum>linenum)
  400.           {
  401.                *posptr=(*posptr)->previous;
  402.           }
  403.           else if ((*posptr)->linenum<linenum)
  404.           {
  405.                *posptr=(*posptr)->next;
  406.           }
  407.      }
  408. }
  409.  
  410. void writescreenfull(char width,char height,nodetypeptr posptr,char stringpos)
  411. {
  412.      char y;
  413.  
  414.      window(1,1,width,height);
  415.      clrscr();
  416.      window(1,1,width,height+1);
  417.      y=1;
  418.      while ((y<=height) && (!empty(posptr->next)))
  419.      {
  420.           gotoxy(1,y);
  421.           strcpy(line_,posptr->line);
  422.           line__=&(line_[stringpos]);
  423.           strncpy(line,line__,width-1);
  424.           strcat(line,null);
  425.           cputs(line);
  426.           posptr=posptr->next;
  427.           y++;
  428.      }
  429. }
  430.  
  431. void removelineends(char *strings,unsigned int maxstrings)
  432. {
  433.      unsigned int i;
  434.  
  435.      i=0;
  436.      while ((i<maxstrings) && (strings[i]!=lineend))
  437.      {
  438.           i++;
  439.      }
  440.      strings[i]=null;
  441. }
  442.  
  443. void main(int argc,char *argv[])
  444. {
  445.      FILE *file;
  446.      char x,ch,width,height,back,fore,stringpos;
  447.      int numlines,linenum;
  448.      text_info info;
  449.      nodetypeptr listptr,posptr;
  450.  
  451.  
  452.      if (argc<2)
  453.      {
  454.           cputs("\nShow what . . .\n");
  455.      }
  456.      else
  457.      {
  458.           file=fopen(argv[1],"rt");
  459.           if (file==NULL)
  460.           {
  461.                cprintf("\nFile %s not found . . .\n",argv[1]);
  462.           }
  463.           else
  464.           {
  465.                rewind(file);
  466.                initialize(&listptr);
  467.                linenum=0;
  468.                while (fgets(line,maxstring,file)!=NULL)
  469.                {
  470.                     removelineends(line,maxstring);
  471.                     add(&listptr,line,linenum);
  472.                     linenum++;
  473.                }
  474.                numlines=linenum--;
  475.                fclose(file);
  476.                posptr=listptr;
  477.                gettextinfo(&info);
  478.                fore=info.attribute&0x0f;
  479.                back=(info.attribute&0x70)/0x10;
  480.                height=info.screenheight;
  481.                width=info.screenwidth;
  482.                strcpy(bottomline,"  \30  \31  pgup  pgdn  s <string> (search for string)  esc (quit)");
  483.                for(x=strlen(bottomline)+1;x<width;x++)
  484.                {
  485.                     strcat(bottomline," ");
  486.                }
  487.                _setcursortype(_NOCURSOR);
  488.                clrscr();
  489.                textcolor(back);
  490.                textbackground(fore);
  491.                gotoxy(1,height);
  492.                cputs(bottomline);
  493.                textcolor(fore);
  494.                textbackground(back);
  495.                height--;
  496.                linenum=1;
  497.                stringpos=1;
  498.                writescreenfull(width,height,posptr,stringpos);
  499.                ch=space;
  500.                while (ch!=escape)
  501.                {
  502.                     while ((ch!=up) && (ch!=down) && (ch!=pageup) &&
  503.                            (ch!=pagedown) && (ch!=escape) && (ch!=left) &&
  504.                            (ch!=right) && (ch!=end_) && (ch!=home))
  505.                     {
  506.                          ch=getch();
  507.                          if (ch==null)
  508.                          {
  509.                               ch=getch();
  510.                          }
  511.                     }
  512.                     if (ch==up)
  513.                     {
  514.                          if ((linenum-1)>=1)
  515.                          {
  516.                               linenum--;
  517.                          }
  518.                          else
  519.                          {
  520.                               linenum=1;
  521.                          }
  522.                          setpos(&posptr,linenum);
  523.  
  524.                     }
  525.                     else if (ch==down)
  526.                     {
  527.                          if ((linenum+1+height)<=numlines)
  528.                          {
  529.                               linenum++;
  530.                          }
  531.                          else
  532.                          {
  533.                               linenum=numlines-height+1;
  534.                          }
  535.                          setpos(&posptr,linenum);
  536.                     }
  537.                     else if (ch==pageup)
  538.                     {
  539.                          if ((linenum-height+1)>=1)
  540.                          {
  541.                               linenum=linenum-height+1;
  542.                          }
  543.                          else
  544.                          {
  545.                               linenum=1;
  546.                          }
  547.                          setpos(&posptr,linenum);
  548.                     }
  549.                     else if (ch==pagedown)
  550.                     {
  551.                          if (((linenum+height-1)<=numlines) && ((numlines-(linenum+height-1))>=(height-1)))
  552.                          {
  553.                               linenum=linenum+height-1;
  554.                          }
  555.                          else
  556.                          {
  557.                               linenum=numlines-height+1;
  558.                          }
  559.                          setpos(&posptr,linenum);
  560.                     }
  561.                     else if (ch==left)
  562.                     {
  563.                          if (stringpos>1)
  564.                          {
  565.                               stringpos--;
  566.                          }
  567.                     }
  568.                     else if (ch==right)
  569.                     {
  570.                          if (stringpos<=(maxstring-width))
  571.                          {
  572.                               stringpos++;
  573.                          }
  574.                     }
  575.                     else if (ch==end_)
  576.                     {
  577.                          stringpos=maxstring-width+1;
  578.                     }
  579.                     else if (ch==home)
  580.                     {
  581.                          stringpos=1;
  582.                     }
  583.                     if (ch!=escape)
  584.                     {
  585.                          ch=space;
  586.                     }
  587.                     writescreenfull(width,height,posptr,stringpos);
  588.                }
  589.                height+=1;
  590.                window(1,1,width,height);
  591.                clrscr();
  592.                _setcursortype(_NORMALCURSOR);
  593.                while (!empty(listptr))
  594.                {
  595.                     remove(&listptr);
  596.                }
  597.           }
  598.      }
  599. }
  600.